package com.hq.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hq.bean.Brand;
import com.hq.bean.Spu;
import com.hq.bean.Typess;
import com.hq.service.SpuService;
import com.hq.util.FileUploadUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SpuController {
    @Resource
    private SpuService service;

    @RequestMapping("list.do")
    public String list(@RequestParam(value = "pages",defaultValue = "1")Integer pages, String sname, String title, Integer bid, String xx, String px, Model model){
        PageHelper.startPage(pages,3);
        HashMap map = new HashMap();
        map.put("sname",sname);
        map.put("title",title);
        map.put("bid",bid);
        map.put("xx",xx);
        map.put("px",px);
        List<Spu> list = service.list(map);
        PageInfo<Spu> pageInfo = new PageInfo<>(list);
        model.addAttribute("map",map);
        model.addAttribute("pageInfo",pageInfo);
        return "list";
    }

    @RequestMapping("brand.do")
    @ResponseBody
    public Object brand(){
        List<Brand> brand = service.brand();
        return brand;
    }

    @RequestMapping("typess.do")
    @ResponseBody
    public Object typess(Integer pid){
        List<Typess> brand = service.typess(pid);
        return brand;
    }

    @RequestMapping("toadd.do")
    public String toadd(){
        return "add";
    }

    @RequestMapping("add.do")
    @ResponseBody
    public Object add(Spu spu, MultipartFile photo, HttpSession session){
        String upload = FileUploadUtil.load(session, photo, "upload");
        spu.setPic(upload);
        int add = service.add(spu);
        return add;
    }

    @RequestMapping("del.do")
    @ResponseBody
    public Object del(Integer sid){
        int del = service.del(sid);
        return del;
    }

    @RequestMapping("dels.do")
    @ResponseBody
    public Object dels(String sid){
        int dels = service.dels(sid);
        return dels;
    }

    @RequestMapping("getById.do")
    @ResponseBody
    public Object getById(Integer sid){
        Map byId = service.getById(sid);
        return byId;
    }

    @RequestMapping("toupd.do")
    public String toupd(){
        return "upd";
    }

    @RequestMapping("upd.do")
    @ResponseBody
    public Object upd(Spu spu, MultipartFile photo, HttpSession session){
        String upload = FileUploadUtil.load(session, photo, "upload");
        spu.setPic(upload);
        int add = service.upd(spu);
        return add;
    }
}
